package cn.aylog.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
@PropertySource("classpath:custom.properties")
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Value("${interceptor}")
    private String interceptor;

    @Value("${pattern}")
    private String pattern;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        try {
            Class<HandlerInterceptor> clazz =
                    (Class<HandlerInterceptor>) Class.forName(interceptor);
            HandlerInterceptor o = clazz.newInstance();
            registry.addInterceptor(o).addPathPatterns(pattern);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
